﻿using System;
using System.Text;
using System.Windows.Forms;

namespace DocumentCommon
{
    public class Utilities
    {
        public static void CloseForm(Form current, Form destination)
        {
            destination.MdiParent = current.MdiParent;
            destination.Dock = DockStyle.Fill;

            current.Hide();
            destination.Show();
            current.Close();
        }

        public static void ResetBackColor(MenuStrip menu, int selectedIndex)
        {
            //foreach (ToolStripItem item in menu.Items)
            //{
            //    item.ResetBackColor();
            //}

            //menu.Items[selectedIndex].BackColor = Color.FromArgb(194, 224, 255);
        }

        public static void ShowMdi(Form current)
        {
            current.Hide();
            var controls = current.MdiParent.Controls.Find("mainPanel", true);

            if(controls.Length > 0)
            {
                controls[0].Visible = true;
            }
            current.Close();
        }

        public static bool OpenForm(Form mdi, string currentFormName)
        {
            if (mdi.ActiveControl != null)
            {
                if (!string.Equals(mdi.ActiveControl.Name, currentFormName, StringComparison.InvariantCulture))
                {
                    if (mdi.ActiveControl is Form)
                    {
                        ((Form)mdi.ActiveControl).Close();
                    }
                    return true;
                }
                return false;
            }
            return true;
        }
        
        public static string EncodePassword(string password)
        {
            byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(password);
            string encoded = Convert.ToBase64String(toEncodeAsBytes);

            return encoded;
        }

        public static string DecodedPassword(string encodedPassword)
        {
            byte[] encodedDataAsBytes = Convert.FromBase64String(encodedPassword);
            string password = Encoding.ASCII.GetString(encodedDataAsBytes);

            return password;
        }        
    }
}
